
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.apps.registry &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-apps-registry">
            
  <h1>Source code for django.apps.registry</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">Counter</span><span class="p">,</span> <span class="n">OrderedDict</span><span class="p">,</span> <span class="n">defaultdict</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">partial</span>

<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">AppRegistryNotReady</span><span class="p">,</span> <span class="n">ImproperlyConfigured</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">lru_cache</span>

<span class="kn">from</span> <span class="nn">.config</span> <span class="k">import</span> <span class="n">AppConfig</span>


<span class="k">class</span> <span class="nc">Apps</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A registry that stores the configuration of installed applications.</span>

<span class="sd">    It also keeps track of models eg. to provide reverse-relations.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">installed_apps</span><span class="o">=</span><span class="p">()):</span>
        <span class="c1"># installed_apps is set to None when creating the master registry</span>
        <span class="c1"># because it cannot be populated at that point. Other registries must</span>
        <span class="c1"># provide a list of installed apps and are populated immediately.</span>
        <span class="k">if</span> <span class="n">installed_apps</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="vm">__name__</span><span class="p">],</span> <span class="s1">&#39;apps&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;You must supply an installed_apps argument.&quot;</span><span class="p">)</span>

        <span class="c1"># Mapping of app labels =&gt; model names =&gt; model classes. Every time a</span>
        <span class="c1"># model is imported, ModelBase.__new__ calls apps.register_model which</span>
        <span class="c1"># creates an entry in all_models. All imported models are registered,</span>
        <span class="c1"># regardless of whether they&#39;re defined in an installed application</span>
        <span class="c1"># and whether the registry has been populated. Since it isn&#39;t possible</span>
        <span class="c1"># to reimport a module safely (it could reexecute initialization code)</span>
        <span class="c1"># all_models is never overridden or reset.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">all_models</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="n">OrderedDict</span><span class="p">)</span>

        <span class="c1"># Mapping of labels to AppConfig instances for installed apps.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>

        <span class="c1"># Stack of app_configs. Used to store the current state in</span>
        <span class="c1"># set_available_apps and set_installed_apps.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stored_app_configs</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Whether the registry is populated.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apps_ready</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">models_ready</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1"># Lock for thread-safe population.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>

        <span class="c1"># Maps (&quot;app_label&quot;, &quot;modelname&quot;) tuples to lists of functions to be</span>
        <span class="c1"># called when the corresponding model is ready. Used by this class&#39;s</span>
        <span class="c1"># `lazy_model_operation()` and `do_pending_operations()` methods.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_pending_operations</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>

        <span class="c1"># Populate apps and models, unless it&#39;s the master registry.</span>
        <span class="k">if</span> <span class="n">installed_apps</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">populate</span><span class="p">(</span><span class="n">installed_apps</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">populate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">installed_apps</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Loads application configurations and models.</span>

<span class="sd">        This method imports each application module and then each model module.</span>

<span class="sd">        It is thread safe and idempotent, but not reentrant.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="c1"># populate() might be called by two threads in parallel on servers</span>
        <span class="c1"># that create threads before initializing the WSGI callable.</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span><span class="p">:</span>
                <span class="k">return</span>

            <span class="c1"># app_config should be pristine, otherwise the code below won&#39;t</span>
            <span class="c1"># guarantee that the order matches the order in INSTALLED_APPS.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;populate() isn&#39;t reentrant&quot;</span><span class="p">)</span>

            <span class="c1"># Phase 1: initialize app configs and import app modules.</span>
            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">installed_apps</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">AppConfig</span><span class="p">):</span>
                    <span class="n">app_config</span> <span class="o">=</span> <span class="n">entry</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">app_config</span> <span class="o">=</span> <span class="n">AppConfig</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">app_config</span><span class="o">.</span><span class="n">label</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                        <span class="s2">&quot;Application labels aren&#39;t unique, &quot;</span>
                        <span class="s2">&quot;duplicates: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">app_config</span><span class="o">.</span><span class="n">label</span><span class="p">)</span>

                <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="p">[</span><span class="n">app_config</span><span class="o">.</span><span class="n">label</span><span class="p">]</span> <span class="o">=</span> <span class="n">app_config</span>
                <span class="n">app_config</span><span class="o">.</span><span class="n">apps</span> <span class="o">=</span> <span class="bp">self</span>

            <span class="c1"># Check for duplicate app names.</span>
            <span class="n">counts</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span>
                <span class="n">app_config</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
            <span class="n">duplicates</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">name</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">count</span> <span class="ow">in</span> <span class="n">counts</span><span class="o">.</span><span class="n">most_common</span><span class="p">()</span> <span class="k">if</span> <span class="n">count</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">duplicates</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                    <span class="s2">&quot;Application names aren&#39;t unique, &quot;</span>
                    <span class="s2">&quot;duplicates: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">duplicates</span><span class="p">))</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">apps_ready</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="c1"># Phase 2: import models modules.</span>
            <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
                <span class="n">app_config</span><span class="o">.</span><span class="n">import_models</span><span class="p">()</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">models_ready</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="c1"># Phase 3: run ready() methods of app configs.</span>
            <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">():</span>
                <span class="n">app_config</span><span class="o">.</span><span class="n">ready</span><span class="p">()</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">check_apps_ready</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Raises an exception if all apps haven&#39;t been imported yet.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">apps_ready</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">AppRegistryNotReady</span><span class="p">(</span><span class="s2">&quot;Apps aren&#39;t loaded yet.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">check_models_ready</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Raises an exception if all models haven&#39;t been imported yet.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">models_ready</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">AppRegistryNotReady</span><span class="p">(</span><span class="s2">&quot;Models aren&#39;t loaded yet.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_app_configs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Imports applications and returns an iterable of app configs.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_apps_ready</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_app_config</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Imports applications and returns an app config for the given label.</span>

<span class="sd">        Raises LookupError if no application exists with this label.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_apps_ready</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="p">[</span><span class="n">app_label</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">message</span> <span class="o">=</span> <span class="s2">&quot;No installed app with label &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="n">app_label</span>
            <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">app_config</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">app_label</span><span class="p">:</span>
                    <span class="n">message</span> <span class="o">+=</span> <span class="s2">&quot; Did you mean &#39;</span><span class="si">%s</span><span class="s2">&#39;?&quot;</span> <span class="o">%</span> <span class="n">app_config</span><span class="o">.</span><span class="n">label</span>
                    <span class="k">break</span>
            <span class="k">raise</span> <span class="ne">LookupError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

    <span class="c1"># This method is performance-critical at least for Django&#39;s test suite.</span>
    <span class="nd">@lru_cache</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_models</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">include_auto_created</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">include_swapped</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of all installed models.</span>

<span class="sd">        By default, the following models aren&#39;t included:</span>

<span class="sd">        - auto-created models for many-to-many relations without</span>
<span class="sd">          an explicit intermediate table,</span>
<span class="sd">        - models that have been swapped out.</span>

<span class="sd">        Set the corresponding keyword argument to True to include such models.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_models_ready</span><span class="p">()</span>

        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">result</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_auto_created</span><span class="p">,</span> <span class="n">include_swapped</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">result</span>

    <span class="k">def</span> <span class="nf">get_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">require_ready</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the model matching the given app_label and model_name.</span>

<span class="sd">        As a shortcut, this function also accepts a single argument in the</span>
<span class="sd">        form &lt;app_label&gt;.&lt;model_name&gt;.</span>

<span class="sd">        model_name is case-insensitive.</span>

<span class="sd">        Raises LookupError if no application exists with this label, or no</span>
<span class="sd">        model exists with this name in the application. Raises ValueError if</span>
<span class="sd">        called with a single argument that doesn&#39;t contain exactly one dot.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">require_ready</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_models_ready</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_apps_ready</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">model_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span> <span class="o">=</span> <span class="n">app_label</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>

        <span class="n">app_config</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_app_config</span><span class="p">(</span><span class="n">app_label</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">require_ready</span> <span class="ow">and</span> <span class="n">app_config</span><span class="o">.</span><span class="n">models</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">app_config</span><span class="o">.</span><span class="n">import_models</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">app_config</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">require_ready</span><span class="o">=</span><span class="n">require_ready</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">register_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="c1"># Since this method is called when models are imported, it cannot</span>
        <span class="c1"># perform imports because of the risk of import loops. It mustn&#39;t</span>
        <span class="c1"># call get_app_config().</span>
        <span class="n">model_name</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
        <span class="n">app_models</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">all_models</span><span class="p">[</span><span class="n">app_label</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">model_name</span> <span class="ow">in</span> <span class="n">app_models</span><span class="p">:</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="n">app_models</span><span class="p">[</span><span class="n">model_name</span><span class="p">]</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">and</span>
                    <span class="n">model</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">==</span> <span class="n">app_models</span><span class="p">[</span><span class="n">model_name</span><span class="p">]</span><span class="o">.</span><span class="vm">__module__</span><span class="p">):</span>
                <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s2">&quot;Model &#39;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&#39; was already registered. &quot;</span>
                    <span class="s2">&quot;Reloading models is not advised as it can lead to inconsistencies, &quot;</span>
                    <span class="s2">&quot;most notably with related models.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">),</span>
                    <span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                    <span class="s2">&quot;Conflicting &#39;</span><span class="si">%s</span><span class="s2">&#39; models in application &#39;</span><span class="si">%s</span><span class="s2">&#39;: </span><span class="si">%s</span><span class="s2"> and </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">app_models</span><span class="p">[</span><span class="n">model_name</span><span class="p">],</span> <span class="n">model</span><span class="p">))</span>
        <span class="n">app_models</span><span class="p">[</span><span class="n">model_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">do_pending_operations</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">is_installed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Checks whether an application with this name exists in the registry.</span>

<span class="sd">        app_name is the full name of the app eg. &#39;django.contrib.admin&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_apps_ready</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">ac</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">app_name</span> <span class="k">for</span> <span class="n">ac</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">get_containing_app_config</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">object_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Look for an app config containing a given object.</span>

<span class="sd">        object_name is the dotted Python path to the object.</span>

<span class="sd">        Returns the app config for the inner application in case of nesting.</span>
<span class="sd">        Returns None if the object isn&#39;t in any registered app config.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_apps_ready</span><span class="p">()</span>
        <span class="n">candidates</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">object_name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
                <span class="n">subpath</span> <span class="o">=</span> <span class="n">object_name</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">name</span><span class="p">):]</span>
                <span class="k">if</span> <span class="n">subpath</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span> <span class="ow">or</span> <span class="n">subpath</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;.&#39;</span><span class="p">:</span>
                    <span class="n">candidates</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">app_config</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">candidates</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">candidates</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">ac</span><span class="p">:</span> <span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">ac</span><span class="o">.</span><span class="n">name</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_registered_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Similar to get_model(), but doesn&#39;t require that an app exists with</span>
<span class="sd">        the given app_label.</span>

<span class="sd">        It&#39;s safe to call this method at import time, even while the registry</span>
<span class="sd">        is being populated.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">all_models</span><span class="p">[</span><span class="n">app_label</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">model_name</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">model</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">LookupError</span><span class="p">(</span>
                <span class="s2">&quot;Model &#39;</span><span class="si">%s</span><span class="s2">.</span><span class="si">%s</span><span class="s2">&#39; not registered.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">model</span>

    <span class="nd">@lru_cache</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">get_swappable_settings_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_string</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        For a given model string (e.g. &quot;auth.User&quot;), return the name of the</span>
<span class="sd">        corresponding settings name if it refers to a swappable model. If the</span>
<span class="sd">        referred model is not swappable, return None.</span>

<span class="sd">        This method is decorated with lru_cache because it&#39;s performance</span>
<span class="sd">        critical when it comes to migrations. Since the swappable settings don&#39;t</span>
<span class="sd">        change after Django has loaded the settings, there is no reason to get</span>
<span class="sd">        the respective settings attribute over and over again.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_swapped</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="n">swapped</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swapped</span>
            <span class="c1"># Is this model swapped out for the model given by to_string?</span>
            <span class="k">if</span> <span class="n">swapped</span> <span class="ow">and</span> <span class="n">swapped</span> <span class="o">==</span> <span class="n">to_string</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span>
            <span class="c1"># Is this model swappable and the one given by to_string?</span>
            <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span> <span class="ow">and</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label</span> <span class="o">==</span> <span class="n">to_string</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">swappable</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">set_available_apps</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">available</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Restricts the set of installed apps used by get_app_config[s].</span>

<span class="sd">        available must be an iterable of application names.</span>

<span class="sd">        set_available_apps() must be balanced with unset_available_apps().</span>

<span class="sd">        Primarily used for performance optimization in TransactionTestCase.</span>

<span class="sd">        This method is safe is the sense that it doesn&#39;t trigger any imports.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">available</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">available</span><span class="p">)</span>
        <span class="n">installed</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">())</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">available</span><span class="o">.</span><span class="n">issubset</span><span class="p">(</span><span class="n">installed</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Available apps isn&#39;t a subset of installed apps, extra apps: </span><span class="si">%s</span><span class="s2">&quot;</span>
                <span class="o">%</span> <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">available</span> <span class="o">-</span> <span class="n">installed</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">stored_app_configs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">(</span>
            <span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">app_config</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">label</span><span class="p">,</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">app_config</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="n">available</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">unset_available_apps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancels a previous call to set_available_apps().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stored_app_configs</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">set_installed_apps</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">installed</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Enables a different set of installed apps for get_app_config[s].</span>

<span class="sd">        installed must be an iterable in the same format as INSTALLED_APPS.</span>

<span class="sd">        set_installed_apps() must be balanced with unset_installed_apps(),</span>
<span class="sd">        even if it exits with an exception.</span>

<span class="sd">        Primarily used as a receiver of the setting_changed signal in tests.</span>

<span class="sd">        This method may trigger new imports, which may add new models to the</span>
<span class="sd">        registry of all imported models. They will stay in the registry even</span>
<span class="sd">        after unset_installed_apps(). Since it isn&#39;t possible to replay</span>
<span class="sd">        imports safely (eg. that could lead to registering listeners twice),</span>
<span class="sd">        models are registered when they&#39;re imported and never removed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">AppRegistryNotReady</span><span class="p">(</span><span class="s2">&quot;App registry isn&#39;t ready yet.&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stored_app_configs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apps_ready</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">models_ready</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">populate</span><span class="p">(</span><span class="n">installed</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">unset_installed_apps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancels a previous call to set_installed_apps().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stored_app_configs</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">apps_ready</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">models_ready</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear_cache</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">clear_cache</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Clears all internal caches, for methods that alter the app registry.</span>

<span class="sd">        This is mostly used in tests.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Call expire cache on each model. This will purge</span>
        <span class="c1"># the relation tree and the fields cache.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">get_models</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ready</span><span class="p">:</span>
            <span class="c1"># Circumvent self.get_models() to prevent that the cache is refilled.</span>
            <span class="c1"># This particularly prevents that an empty value is cached while cloning.</span>
            <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_configs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
                <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">app_config</span><span class="o">.</span><span class="n">get_models</span><span class="p">(</span><span class="n">include_auto_created</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
                    <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_expire_cache</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">lazy_model_operation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="o">*</span><span class="n">model_keys</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Take a function and a number of (&quot;app_label&quot;, &quot;modelname&quot;) tuples, and</span>
<span class="sd">        when all the corresponding models have been imported and registered,</span>
<span class="sd">        call the function with the model classes as its arguments.</span>

<span class="sd">        The function passed to this method must accept exactly n models as</span>
<span class="sd">        arguments, where n=len(model_keys).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Base case: no arguments, just execute the function.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">model_keys</span><span class="p">:</span>
            <span class="n">function</span><span class="p">()</span>
        <span class="c1"># Recursive case: take the head of model_keys, wait for the</span>
        <span class="c1"># corresponding model class to be imported and registered, then apply</span>
        <span class="c1"># that argument to the supplied function. Pass the resulting partial</span>
        <span class="c1"># to lazy_model_operation() along with the remaining model args and</span>
        <span class="c1"># repeat until all models are loaded and all arguments are applied.</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">next_model</span><span class="p">,</span> <span class="n">more_models</span> <span class="o">=</span> <span class="n">model_keys</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">model_keys</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>

            <span class="c1"># This will be executed after the class corresponding to next_model</span>
            <span class="c1"># has been imported and registered. The `func` attribute provides</span>
            <span class="c1"># duck-type compatibility with partials.</span>
            <span class="k">def</span> <span class="nf">apply_next_model</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
                <span class="n">next_function</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">apply_next_model</span><span class="o">.</span><span class="n">func</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">lazy_model_operation</span><span class="p">(</span><span class="n">next_function</span><span class="p">,</span> <span class="o">*</span><span class="n">more_models</span><span class="p">)</span>
            <span class="n">apply_next_model</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">function</span>

            <span class="c1"># If the model has already been imported and registered, partially</span>
            <span class="c1"># apply it to the function now. If not, add it to the list of</span>
            <span class="c1"># pending operations for the model, where it will be executed with</span>
            <span class="c1"># the model class as its sole argument once the model is ready.</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">model_class</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_registered_model</span><span class="p">(</span><span class="o">*</span><span class="n">next_model</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">LookupError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_pending_operations</span><span class="p">[</span><span class="n">next_model</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">apply_next_model</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">apply_next_model</span><span class="p">(</span><span class="n">model_class</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">do_pending_operations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Take a newly-prepared model and pass it to each function waiting for</span>
<span class="sd">        it. This is called at the very end of `Apps.register_model()`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span>
        <span class="k">for</span> <span class="n">function</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pending_operations</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="p">[]):</span>
            <span class="n">function</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>


<span class="n">apps</span> <span class="o">=</span> <span class="n">Apps</span><span class="p">(</span><span class="n">installed_apps</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>